package com.xudow.app.ui.user;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.activeshare.edu.ucenter.models.coupons.CouponsWithOther;
import com.xudow.app.R;
import com.xudow.app.ui.BaseActivity;
import com.xudow.app.ui.adapter.user.CouponAdapter;
import com.xudow.app.ui.task.CouponListTask;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 优惠券 Activity
 *
 * @author lukecheng
 */
public class CouponsActivity extends BaseActivity {

    private CouponListTask couponListTask;

    private List<CouponsWithOther> coupons;

    private ListView listView;
//    private ProgressBar progressBar;

    private CouponAdapter couponAdapter;

    LinearLayout date_rl;
    TextView date_txt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_coupons);
        date_rl=(LinearLayout)findViewById(R.id.date_rl);
        date_txt=(TextView)findViewById(R.id.date_txt);
//        progressBar = (ProgressBar) findViewById(R.id.progressbar);
        listView = (ListView) findViewById(R.id.list);

        couponListTask = new CouponListTask(this, couponListTaskHandler);
        addTask(couponListTask);
        couponListTask.execute();
        showLodingDialog("正在加载...");
        listView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

            }
        });
    }

    public void onBackBtnClick(View view) {
        finish();
    }

    /**
     * 优惠券加载Handler
     */
    private Handler couponListTaskHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            hideLoadingDialog();

            if (CouponListTask.SUCCESS == msg.what) {
                CouponsWithOther[] dataArr = (CouponsWithOther[]) msg.getData().getSerializable("coupons");

                coupons = new ArrayList<CouponsWithOther>();
                coupons.addAll(Arrays.asList(dataArr));

                couponAdapter = new CouponAdapter(CouponsActivity.this,
                        android.R.layout.simple_list_item_1, coupons);
                listView.setAdapter(couponAdapter);
                if(coupons.size()==0){
                    date_rl.setVisibility(View.VISIBLE);
                    date_txt.setText("您还没有优惠券！");
                }


//              progressBar.setVisibility(View.GONE);
                listView.setVisibility(View.VISIBLE);

                couponAdapter.notifyDataSetChanged();
            }
        }
    };

}
